﻿using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using UnityEngine;
using System;

namespace Utility.DataSystem
{
	public struct FieldParamSetting
	{
		public string showName;
		public Type type;

		public FieldParamSetting (string showName, Type type)
		{
			this.showName = showName;
			this.type = type;
		}
	}

	public class FieldActionSettingObject : SettingObject<FieldActionSetting>
	{
		public string key;
		public string showName;
		public int contextFlags;
		public List<FieldParamSetting> paramSettings;
		public int debugMode = 0;
		public bool isContextLocal;
	}

	public class FieldActionSetting : Setting<FieldActionSettingObject>
	{
		protected override void Init ()
		{
			ParamSettings = SettingObject.paramSettings.ToReadOnly ();
		}

		public string Key => SettingObject.key;
		public string ShowName => SettingObject.showName;
		public int ContextFlags => SettingObject.contextFlags;
		public ReadOnlyCollection<FieldParamSetting> ParamSettings { get; private set; }
		public int ParamCount => ParamSettings.Count;
		public int DebugMode => SettingObject.debugMode;
		public bool IsContextLocal => SettingObject.isContextLocal;

		public void PerformAction (IDataFieldContext context, IList<DataValue> parameters)
		{
			var a = new FieldAction (this, context, parameters);
			context.OnAction (a);
		}
	}

	public struct FieldAction
	{
		public FieldAction (FieldActionSetting setting, IDataFieldContext sourceContext, IList<DataValue> parameters, int targetEntities = 0)
		{
			Setting = setting;
			SourceContext = sourceContext;
			Parameters = parameters;
			TargetEntities = targetEntities;
		}

		public FieldAction (FieldActionSetting setting, IDataFieldContext sourceContext, int targetEntities = 0) : this (setting, sourceContext, null, targetEntities)
		{
		}

		public FieldActionSetting Setting { get; }
		public IDataFieldContext SourceContext { get; }
		public int TargetEntities { get; }
		public IList<DataValue> Parameters { get; }

		public bool HasParam => (Parameters?.Count ?? 0) > 0;
	}
}